﻿using Sustainalytics.Synchronization.Interface;
using System;

namespace Sustainalytics.ClientDW.Synchronization.SyncRiskRatingsData
{
    public class RiskRatingsDataSyncStep : ISyncStep
    {
        public SyncStepOrder ExecutionOrder => SyncStepOrder.SyncRiskRatingsData;

        public SyncContext Start(SyncContext syncContext)
        {
            try
            {
                AutoMapperConfig.RegisterMappings();
                var container = SimpleInjectorConfig.Register(new SyncContextLogger(syncContext), syncContext.GAConnectionStrings);


                var coreRiskRatingsProfilesUpdated = container.GetInstance<CoreRiskRatingsSync>().StartAsync().Result;

                if (coreRiskRatingsProfilesUpdated == 0)
                {
                    syncContext.Log("Updated 0 (zero) core risk rating profiles because there are no companies that meet the criteria");
                    syncContext.Status = SyncOperationStatus.Failed;
                }


                var riskRatingsProfilesUpdated = container.GetInstance<RiskRatingsSync>().StartAsync(false).Result;

                if (riskRatingsProfilesUpdated == 0)
                {
                    syncContext.Log("Updated 0 (zero) risk rating profiles because there are no companies that meet the criteria");
                    syncContext.Status = SyncOperationStatus.Failed;
                }
            }
            catch (Exception exception)
            {
                syncContext.Log(exception.ToString());
                syncContext.Status = SyncOperationStatus.Failed;
                throw;
            }

            return syncContext;
        }
    }
}
